package com.example.qcweb;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dao.DBDao;
import com.example.modal.ExamineItem;
import com.example.modal.MostPortItem;
import com.example.modal.MultimediaDemand;
import com.example.modal.MultimediaRecord;
import com.example.modal.User;
import com.example.modal.WorkingProcedure;
import com.example.qc.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class OperateActivity extends Activity {
    int position;
    private String SystemId, activityname, controlname;
    private final int REQUEST_CODE = 1;
    private Intent myintent;
    private String tabletype;
    private User user;

    public class Select implements OnClickListener {

        public Select(int pos) {
            super();
            position = pos;
            // TODO Auto-generated constructor stub
        }

        private boolean isSort;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (isSort) {
                return;
            }
            if (selectMap.get(position) == "1") {
                SelectedAll = false;
                selectMap.put(position, "0");

            } else {
                selectMap.put(position, "1");
            }
            switch (varTableType) {
                case TablePart:
                    padapter.notifyDataSetChanged();
                    break;
                case TableExam:
                    Eadapter.notifyDataSetChanged();

                    break;
                case TableMultiMedia:
                    madapter.notifyDataSetChanged();
                    break;

                default:
                    break;
            }
        }

    }

    private static final int DIALOG_SureDelView = 0;
    public static final int Dialog_SureAdd = 1;
    public static final int CodeCardView = 2;
    public static final int DIALOG_SureCopy = 3;

    public List<Integer> GetselectKey() {
        List<Integer> selectArray = new ArrayList<Integer>();
        SelectedAll = false;

        for (int i : selectMap.keySet()) {
            if (selectMap.get(i).equalsIgnoreCase("1")) {
                selectArray.add(i);

            }
        }
        return selectArray;
    }

    public class MultiMediaAdapter extends BaseAdapter {
        public class ViewHolder {
            public TextView multinumberTextView;
            public ImageButton imgbtnMd;
            public TextView posiTextView;
            public TextView photoView;
            public TextView contentView;
            public TextView photonoview;
            public TextView moperatorView;
            public TextView mcheckerView;
            public TextView mtechnicView;

        }

        LayoutInflater inflater;

        public MultiMediaAdapter(Context context) {
            super();
            inflater = LayoutInflater.from(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (null != multimediaDemands) {
                return multimediaDemands.size();
            } else {
                return 0;

            }
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(
                        R.layout.activity_new_mediarecordcell, null);
                viewHolder = new ViewHolder();
                viewHolder.multinumberTextView = (TextView) convertView
                        .findViewById(R.id.tvmnumber);
                viewHolder.posiTextView = (TextView) convertView
                        .findViewById(R.id.tvposition);
                viewHolder.photoView = (TextView) convertView
                        .findViewById(R.id.tvphoto);
                viewHolder.contentView = (TextView) convertView
                        .findViewById(R.id.tvcontent);
                viewHolder.photonoview = (TextView) convertView
                        .findViewById(R.id.tvphotono);
                viewHolder.moperatorView = (TextView) convertView
                        .findViewById(R.id.tvmoperator);
                viewHolder.mcheckerView = (TextView) convertView
                        .findViewById(R.id.tvmchecker);
                viewHolder.mtechnicView = (TextView) convertView
                        .findViewById(R.id.tvmtechnic);
                viewHolder.imgbtnMd = (ImageButton) convertView
                        .findViewById(R.id.imgbtnMdselect);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();

            }
            MultimediaRecord multimediaRecord = multimediaRecords.get(position);
            MultimediaDemand multimediaDemand = multimediaDemands.get(position);
            // 序号
            viewHolder.multinumberTextView.setText(multimediaDemand
                    .getSystemId());
            // 部位
            viewHolder.posiTextView.setText(multimediaDemand
                    .getProductPosition());
            // 照相/摄像
            String strType = null;
            if (multimediaDemand.getMultimediaType() == "1") {
                strType = "照相";

            } else if (multimediaDemand.getMultimediaType() == "2") {
                strType = "摄像";

            }
            viewHolder.photoView.setText(strType);
            // 内容
            viewHolder.contentView.setText(multimediaDemand.getRecordContent());
            // 照片编号
            viewHolder.photonoview.setText(multimediaRecord
                    .getMultimediaNumber());
            // 操作者
            viewHolder.moperatorView.setText(multimediaRecord.getRecorder());
            // 检验员
            viewHolder.mcheckerView.setText(multimediaRecord.getVerifier());
            // 工艺员
            viewHolder.mtechnicView.setText(multimediaRecord.getTechnic());
            viewHolder.imgbtnMd.setOnClickListener(new Select(position));
            if (selectMap.get(position) == "1") {

                viewHolder.imgbtnMd.setImageDrawable(getResources()
                        .getDrawable(R.drawable.ic_launcher));
            } else {
                viewHolder.imgbtnMd.setImageDrawable(getResources()
                        .getDrawable(R.drawable.iconpart));
            }
            return convertView;
        }

    }

    public class PartAdapter extends BaseAdapter {
        public class ViewHolder {
            public TextView xuhao;
            public ImageButton imgbtnPartselect;
            public TextView mingcheng;
            public TextView tuhao;
            public TextView lingjianbianhao;
            public TextView canshudaihao;
            public TextView beizhu;
            public TextView caozuozhe;
            public TextView hujianzhe;
            public TextView jianyanyuan;

        }

        LayoutInflater inflater;

        public PartAdapter(Context context) {
            super();
            inflater = LayoutInflater.from(context);

            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (null != mostPortItems) {
                return mostPortItems.size();
            } else {
                return 0;

            }
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = inflater.inflate(
                        R.layout.activity_new_partitemcell, null);
                viewHolder = new ViewHolder();
                viewHolder.xuhao = (TextView) convertView
                        .findViewById(R.id.tvno);
                viewHolder.mingcheng = (TextView) convertView
                        .findViewById(R.id.tvpartname);
                viewHolder.tuhao = (TextView) convertView
                        .findViewById(R.id.edrecord);
                viewHolder.lingjianbianhao = (TextView) convertView
                        .findViewById(R.id.tvposition);
                viewHolder.canshudaihao = (TextView) convertView
                        .findViewById(R.id.tvcontent);
                viewHolder.beizhu = (TextView) convertView
                        .findViewById(R.id.tvphotono);
                viewHolder.caozuozhe = (TextView) convertView
                        .findViewById(R.id.tvoperator);
                viewHolder.hujianzhe = (TextView) convertView
                        .findViewById(R.id.tvinterchecker);
                viewHolder.jianyanyuan = (TextView) convertView
                        .findViewById(R.id.tvchecker);
                viewHolder.imgbtnPartselect = (ImageButton) convertView
                        .findViewById(R.id.imgbtnPartselect);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            MostPortItem mPortItem = mostPortItems.get(position);
            viewHolder.xuhao.setText(mPortItem.getSystemId());
            viewHolder.mingcheng.setText(mPortItem.getProductName());
            viewHolder.tuhao.setText(mPortItem.getDrawingNumber());
            viewHolder.lingjianbianhao.setText(mPortItem.getProductNumber());
            viewHolder.canshudaihao.setText(mPortItem.getParaCode());
            viewHolder.beizhu.setText(mPortItem.getRemark());
            viewHolder.caozuozhe.setText(mPortItem.getOperator());
            viewHolder.hujianzhe.setText(mPortItem.getReInnerChecker());
            viewHolder.jianyanyuan.setText(mPortItem.getChecker());

            viewHolder.imgbtnPartselect
                    .setOnClickListener(new Select(position));
            if (selectMap.get(position) == "1") {

                viewHolder.imgbtnPartselect.setImageDrawable(getResources()
                        .getDrawable(R.drawable.ic_launcher));
            } else {
                viewHolder.imgbtnPartselect.setImageDrawable(getResources()
                        .getDrawable(R.drawable.iconpart));
            }
            return convertView;
        }

    }

    public class ExamAdapter extends BaseAdapter {
        public class ViewHolder {
            public TextView ExamNum;
            public ImageButton imgbtnExamselect;
            public TextView jianchjilu;
            public TextView jianchjildemand;
            public TextView record;
            public TextView conclusion;
            public TextView zhizao;
            public TextView remark;
            public TextView operatorTextView;
            public TextView reInterCheckerTextView;
            public TextView checkerTextView;

        }

        LayoutInflater inflater;

        public ExamAdapter(Context context) {
            super();
            inflater = LayoutInflater.from(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (null != examineItems) {
                return examineItems.size();
            } else {
                return 0;

            }
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(
                        R.layout.activity_new_examinecell, null);
                viewHolder = new ViewHolder();
                viewHolder.ExamNum = (TextView) convertView
                        .findViewById(R.id.Examnum);
                // viewHolder.
                viewHolder.jianchjilu = (TextView) convertView
                        .findViewById(R.id.jianchajilu);
                viewHolder.jianchjildemand = (TextView) convertView
                        .findViewById(R.id.jianchjildemand);
                viewHolder.record = (TextView) convertView
                        .findViewById(R.id.record);
                viewHolder.conclusion = (TextView) convertView
                        .findViewById(R.id.conclusion);
                viewHolder.zhizao = (TextView) convertView
                        .findViewById(R.id.tvzhizao);
                viewHolder.remark = (TextView) convertView
                        .findViewById(R.id.remarktext);
                viewHolder.operatorTextView = (TextView) convertView
                        .findViewById(R.id.tvrealexamoperator);
                viewHolder.reInterCheckerTextView = (TextView) convertView
                        .findViewById(R.id.tvexamrechecker);
                viewHolder.checkerTextView = (TextView) convertView
                        .findViewById(R.id.tvexamchecker);
                viewHolder.imgbtnExamselect = (ImageButton) convertView
                        .findViewById(R.id.imgbtnExamselect);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            ExamineItem examineItem = examineItems.get(position);
            viewHolder.ExamNum.setText(examineItem.getSystemId());
            viewHolder.jianchjilu.setText(examineItem.getItem());
            viewHolder.jianchjildemand.setText(examineItem
                    .getCapabilityTarget());
            viewHolder.record.setText(examineItem.getWatchValue());
            viewHolder.conclusion.setText(examineItem.getConclusion());
            viewHolder.zhizao.setText(examineItem.getMakeRes());
            viewHolder.remark.setText(examineItem.getRemark());
            viewHolder.operatorTextView.setText(examineItem.getOperator());
            viewHolder.reInterCheckerTextView.setText(examineItem
                    .getReInterChecker());
            viewHolder.checkerTextView.setText(examineItem.getChecker());
            viewHolder.imgbtnExamselect
                    .setOnClickListener(new Select(position));
            if (selectMap.get(position) == "1") {

                viewHolder.imgbtnExamselect.setImageDrawable(getResources()
                        .getDrawable(R.drawable.ic_launcher));
            } else {
                viewHolder.imgbtnExamselect.setImageDrawable(getResources()
                        .getDrawable(R.drawable.iconpart));
            }

            return convertView;
        }

    }

    boolean SelectedAll;
    WorkingProcedure workProcedure;
    List<ExamineItem> examineItems = new ArrayList<ExamineItem>();
    List<MostPortItem> mostPortItems = new ArrayList<MostPortItem>();
    List<MultimediaRecord> multimediaRecords = new ArrayList<MultimediaRecord>();
    List<MultimediaDemand> multimediaDemands = new ArrayList<MultimediaDemand>();
    HashMap<Integer, String> selectMap = new HashMap<Integer, String>();

    public enum TableType {
        TablePart, TableExam, TableMultiMedia
    }

    ;

    public enum TableAction {
        actionselect, actionDelete, actionCopy, actionAdd, actionCard, actionSort
    }

    ;

    public enum SetCard {
        cardOperator, cardInterChecker, cardChecker, cardTechnic
    }

    ;

    SetCard varSetCard;
    TableAction varTableAction;
    TableType varTableType;
    private String lastSeqNumber;
    private ExamAdapter Eadapter;
    private PartAdapter padapter;
    private MultiMediaAdapter madapter;
    private boolean isTechnic;
    private boolean isData;

    // 左边的操作按钮
    public class clickButton implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.allselect:
                    varTableAction = TableAction.actionselect;
                    actionAllSelect();
                    break;
                case R.id.delete:
                    varTableAction = TableAction.actionDelete;
                    varSetCard = SetCard.cardTechnic;
                    if (GetselectKey().size() > 0) {// 若有选择记录
                        if (isTechnic) {
                            showDialog(DIALOG_SureDelView);
                        } else {
                            TransferID();

                        }
                    } else {// 若没有选择记录
                        Dialog alertDialog = new AlertDialog.Builder(
                                OperateActivity.this).

                                setMessage("请至少选择一条记录").

                                create();
                        alertDialog.show();
                    }
                    // actionDelete();
                    break;
                case R.id.copy:
                    varTableAction = TableAction.actionCopy;
                    varSetCard = SetCard.cardTechnic;
                    if (GetselectKey().size() == 1) {
                        if (isTechnic) {
                            showDialog(DIALOG_SureCopy);
                        } else {
                            TransferID();
                        }
                    } else {
                        unSelectAll();
                        Dialog alertDialog = new AlertDialog.Builder(
                                OperateActivity.this).

                                setMessage("请选择一条记录").

                                create();
                        alertDialog.show();
                    }
                    // actionCopy();
                    break;
                case R.id.add:
                    varTableAction = TableAction.actionAdd;
                    varSetCard = SetCard.cardTechnic;
                    unSelectAll();
                    if (isTechnic) {
                        showDialog(Dialog_SureAdd);
                    } else {
                        TransferID();

                    }
                    // actionAdd();
                    break;
                case R.id.shuaka:

                    // actionShuaka();
                    break;
                case R.id.paixu:
                    unSelectAll();
                    break;

                default:
                    break;
            }
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operate);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        workProcedure = (WorkingProcedure) bundle.getSerializable("key1");
        tabletype = bundle.getString("type");
        user = MyGlobal.user;
        if (MyGlobal.user.getType().endsWith("4")) {
            isTechnic = true;

        } else {
            isTechnic = false;

        }
        if (tabletype.equalsIgnoreCase(String.valueOf("ExamItem"))) {
            varTableType = TableType.TableExam;
        } else if (tabletype.equalsIgnoreCase(String.valueOf("PartItem"))) {
            varTableType = TableType.TablePart;
        } else if (tabletype.equalsIgnoreCase(String.valueOf("MultiItem"))) {
            varTableType = TableType.TableMultiMedia;
        }
        findView();
        reloadTable();

    }

    public void unSelectAll() {
        // TODO Auto-generated method stub
        if (varTableAction == TableAction.actionCard
                || varTableAction == TableAction.actionCopy) {

        } else {
            selectMap.clear();
            if (varTableType == TableType.TablePart) {
                for (int i = 0; i < mostPortItems.size(); i++) {
                    selectMap.put(i, "0");
                }
            } else if (varTableType == TableType.TableExam) {
                for (int i = 0; i < examineItems.size(); i++) {
                    selectMap.put(i, "0");
                }
            } else if (varTableType == TableType.TableMultiMedia) {
                for (int i = 0; i < multimediaDemands.size(); i++) {
                    selectMap.put(i, "0");
                }
            }

        }
    }

    private void reloadTable() {
        // TODO Auto-generated method stub
        switch (varTableType) {
            case TablePart:
                mostPortItems = DBDao
                        .findMostPortItem(workProcedure.getKEYCardCode(),
                                workProcedure.getSystemId());
                if (mostPortItems.size() > 0) {
                    lastSeqNumber = mostPortItems.get(mostPortItems.size() - 1)
                            .getSequenceNumber();
                } else {
                    isData = false;
                    lastSeqNumber = "1";

                }
                padapter.notifyDataSetChanged();
                break;
            case TableExam:
                examineItems = DBDao.findExamItems(workProcedure.getKEYCardCode(),
                        workProcedure.getSystemId());
                if (examineItems.size() > 0) {
                    lastSeqNumber = examineItems.get(examineItems.size() - 1)
                            .getSequenceNumber();
                } else {
                    isData = false;
                    lastSeqNumber = "1";

                }
                Eadapter.notifyDataSetChanged();
                break;
            case TableMultiMedia:
                multimediaRecords = DBDao
                        .findMultimediaRecords(workProcedure.getKEYCardCode(),
                                workProcedure.getSystemId());
                multimediaDemands = DBDao
                        .findMultimediaDemands(workProcedure.getKEYCardCode(),
                                workProcedure.getSystemId());
                if (multimediaDemands.size() > 0) {
                    lastSeqNumber = multimediaDemands.get(
                            multimediaDemands.size() - 1).getSequenceNumber();
                } else {
                    isData = false;
                    lastSeqNumber = "1";

                }
                madapter.notifyDataSetChanged();
                break;

            default:
                break;
        }
        unSelectAll();

    }

    public void actionSort() {
        // TODO Auto-generated method stub

    }

    public void actionShuaka() {
        // TODO Auto-generated method stub

    }

    public void actionAdd() {
        // TODO Auto-generated method stub
        switch (varTableType) {
            case TablePart:
                DBDao.addMPI(workProcedure, lastSeqNumber);

                break;
            case TableExam:
                DBDao.addEI(workProcedure, lastSeqNumber);

                break;
            case TableMultiMedia:
                DBDao.addMD(workProcedure, lastSeqNumber);

                break;
            default:
                break;
        }
    }

    public void actionCopy() {
        // TODO Auto-generated method stub
        List<Integer> keylist = GetselectKey();
        int index = keylist.get(0);
        switch (varTableType) {
            case TableExam:
                ExamineItem eitem = examineItems.get(index);
                DBDao.copyEI(eitem, lastSeqNumber);
                break;
            case TablePart:
                MostPortItem mpi = mostPortItems.get(index);
                DBDao.copyMPI(mpi, lastSeqNumber);
                break;
            case TableMultiMedia:
                MultimediaDemand md = multimediaDemands.get(index);
                MultimediaRecord mr = multimediaRecords.get(index);
                DBDao.copyMD(md, mr, lastSeqNumber);
                break;
            default:
                break;
        }
    }

    public void actionDelete() {
        // TODO Auto-generated method stub
        List<Integer> keylist = GetselectKey();

        switch (varTableType) {
            case TablePart:
                for (int i = 0; i < keylist.size(); i++) {
                    MostPortItem mpiItem = mostPortItems.get(i);
                    if ((mpiItem.getOperator() == null)
                            && (mpiItem.getChecker() == null)
                            && (mpiItem.getReInnerChecker() == null)) {

                    } else {
                        Dialog alertDialog = new AlertDialog.Builder(
                                OperateActivity.this).

                                setMessage(
                                        String.format("操作失败，请检验员取消刷卡\n原因：序号：%s",
                                                mpiItem.getSystemId())).

                                create();
                        alertDialog.show();
                        return;

                    }
                }
                for (int i = 0; i < keylist.size(); i++) {
                    MostPortItem mpiItem = mostPortItems.get(i);
                    DBDao.deleteMPi(mpiItem);
                }
                break;
            case TableExam:
                for (int i = 0; i < keylist.size(); i++) {
                    ExamineItem ei = examineItems.get(i);
                    if ((ei.getOperator() == null) && (ei.getChecker() == null)
                            && (ei.getReInterChecker() == null)) {

                    } else {
                        Dialog alertDialog = new AlertDialog.Builder(
                                OperateActivity.this).

                                setMessage(
                                        String.format("操作失败，请检验员取消刷卡\n原因：序号：%s",
                                                ei.getSystemId())).

                                create();
                        alertDialog.show();
                        return;

                    }
                }
                for (int i = 0; i < keylist.size(); i++) {
                    ExamineItem ei = examineItems.get(i);
                    DBDao.deleteEi(ei);
                }

                break;
            case TableMultiMedia:
                for (int i = 0; i < keylist.size(); i++) {
                    MultimediaRecord mr = multimediaRecords.get(i);
                    if ((mr.getRecorder() == null) && (mr.getVerifier() == null)
                            && (mr.getTechnic() == null)) {

                    } else {
                        Dialog alertDialog = new AlertDialog.Builder(
                                OperateActivity.this).

                                setMessage(
                                        String.format("操作失败，请检验员取消刷卡\n原因：序号：%s",
                                                mr.getSystemId())).

                                create();
                        alertDialog.show();
                        return;

                    }
                }
                for (int i = 0; i < keylist.size(); i++) {
                    MultimediaRecord mr = multimediaRecords.get(i);
                    MultimediaDemand md = multimediaDemands.get(i);
                    DBDao.deleteMd(md, mr);
                }

                break;

            default:
                break;
        }
    }

    public void actionAllSelect() {
        // TODO Auto-generated method stub
        if (!SelectedAll) {
            SelectedAll = true;
            for (int i : selectMap.keySet()) {
                selectMap.put(i, "1");
            }

        } else {
            SelectedAll = false;
            for (int i : selectMap.keySet()) {
                selectMap.put(i, "0");
            }

        }
        switch (varTableType) {
            case TablePart:
                padapter.notifyDataSetChanged();
                break;
            case TableExam:
                Eadapter.notifyDataSetChanged();

                break;
            case TableMultiMedia:
                madapter.notifyDataSetChanged();
                break;

            default:
                break;
        }

    }

    public void doAction() {
        switch (varTableAction) {
            case actionAdd:
                actionAdd();
                break;
            case actionCard:
                actionCard();
                break;
            case actionCopy:
                actionCopy();
                break;
            case actionDelete:
                actionDelete();
                break;
            case actionSort:
                actionSort();
                break;

            default:
                break;
        }
        reloadTable();
    }

    private void actionCard() {
        // TODO Auto-generated method stub

    }

    private void findView() {
        // TODO Auto-generated method stub
        ImageButton AllSelect = (ImageButton) findViewById(R.id.allselect);
        ImageButton deleteButton = (ImageButton) findViewById(R.id.delete);
        ImageButton copyButton = (ImageButton) findViewById(R.id.copy);
        ImageButton addButton = (ImageButton) findViewById(R.id.add);
        ImageButton shuakaButton = (ImageButton) findViewById(R.id.shuaka);
        ImageButton sortButton = (ImageButton) findViewById(R.id.paixu);
        ListView lvmore = (ListView) findViewById(R.id.lvoperate);

        if (varTableType == TableType.TableExam) {
            examineItems = DBDao.findExamItems(workProcedure.getKEYCardCode(),
                    workProcedure.getSystemId());
            Eadapter = new ExamAdapter(OperateActivity.this);
            lvmore.setAdapter(Eadapter);
        } else if (varTableType == TableType.TablePart) {
            mostPortItems = DBDao
                    .findMostPortItem(workProcedure.getKEYCardCode(),
                            workProcedure.getSystemId());
            padapter = new PartAdapter(OperateActivity.this);
            lvmore.setAdapter(padapter);
        } else {
            multimediaRecords = DBDao
                    .findMultimediaRecords(workProcedure.getKEYCardCode(),
                            workProcedure.getSystemId());
            multimediaDemands = DBDao
                    .findMultimediaDemands(workProcedure.getKEYCardCode(),
                            workProcedure.getSystemId());
            madapter = new MultiMediaAdapter(OperateActivity.this);
            lvmore.setAdapter(madapter);

        }
        AllSelect.setOnClickListener(new clickButton());
        deleteButton.setOnClickListener(new clickButton());
        copyButton.setOnClickListener(new clickButton());
        addButton.setOnClickListener(new clickButton());
        shuakaButton.setOnClickListener(new clickButton());
        sortButton.setOnClickListener(new clickButton());
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch (id) {
            case DIALOG_SureDelView:
                return new AlertDialog.Builder(OperateActivity.this)
                        .setTitle("删除")
                        .setMessage("是否删除？")
                        .setCancelable(false)
                        .setPositiveButton(R.string.Dialog_OK,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int id) {
                                        doAction();
                                    }
                                })
                        .setNegativeButton(R.string.Dialog_Cancle,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int id) {
                                        dialog.cancel();
                                    }
                                }).create();

            case Dialog_SureAdd:
                return new AlertDialog.Builder(OperateActivity.this)
                        .setTitle("增加")
                        .setMessage("是否增加？")
                        .setCancelable(false)
                        .setPositiveButton(R.string.Dialog_OK,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int id) {
                                        doAction();
                                    }
                                })
                        .setNegativeButton(R.string.Dialog_Cancle,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int id) {
                                        dialog.cancel();
                                    }
                                }).create();

            case CodeCardView:

                break;
            case DIALOG_SureCopy:
                return new AlertDialog.Builder(OperateActivity.this)
                        .setTitle("复制")
                        .setMessage("是否复制？")
                        .setCancelable(false)
                        .setPositiveButton(R.string.Dialog_OK,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int id) {
                                        doAction();
                                    }
                                })
                        .setNegativeButton(R.string.Dialog_Cancle,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int id) {
                                        dialog.cancel();
                                    }
                                }).create();

        }
        return null;
    }

    // 传递相关参数
    private void TransferID() {
        myintent = new Intent();
        myintent.putExtra("key", workProcedure);
        myintent.putExtra("tabletype", tabletype);
        myintent.putExtra("lastSeqNumber", lastSeqNumber);
        // myAssiIntent.setClass(this, signtoaddactivity.class);
        StartAsignActivity();
    }

    // 根据修改结果更新页面
    private void StartAsignActivity() {
        myintent.setClass(OperateActivity.this, signtoaddactivity.class);
        startActivityForResult(myintent, REQUEST_CODE);
    }

    public void onActivityResult(int requestcode, int reusltcode, Intent intent) {
        super.onActivityResult(requestcode, reusltcode, intent);
        //List<Integer> keylist = GetselectKey();
        // int uflag=data.getIntExtra("uflag", 1);
        // .getExtras().getString("result");
        int uflag = intent.getExtras().getInt("uflag");
        if (uflag == 1) {
            MyGlobal.user = user;
            doAction();
        } else {
            Toast.makeText(this, "你确定更换用户了吗？", Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.operate, menu);
        return true;
    }

}
